﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business.Entity;
using Business.Dal;

namespace Sahibinden.UserControl
{
    public partial class Categories : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                
                GetData(ListBox1, "0");
            }
        }

        public int GetData(ListBox lst, string UstKategoriID)
        {
            KategoriProvider kp = new KategoriProvider();
            List<Kategori> lstKategori = kp.GetUstKategoriler(Convert.ToInt32(UstKategoriID));


            //TODO : TumKategoriID KategoriID yerine kullanılacak
            lst.DataSource = lstKategori;
            lst.DataTextField = "KategoriAdi";
            lst.DataValueField = "TumKategoriID";
            lst.DataBind();

            return lstKategori.Count;

        }

        protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int RecordCount = 0;
            if (((ListBox)sender).ID == "ListBox1")
            {
                RecordCount = GetData(ListBox2, ListBox1.SelectedValue.Split('E')[ListBox1.SelectedValue.Split('E').Length - 2]);
                ListBox2.Visible = true;
                _CategoryID = ListBox1.SelectedValue;
                CategoryID = "0";
                CategoryID = _CategoryID;
                
            }
            else if (((ListBox)sender).ID == "ListBox2")
            {
                RecordCount = GetData(ListBox3, ListBox2.SelectedValue.Split('E')[ListBox2.SelectedValue.Split('E').Length - 2]);
                ListBox3.Visible = true;
                _CategoryID = ListBox2.SelectedValue;
                CategoryID = "0";
                CategoryID = _CategoryID;
            }
            else if (((ListBox)sender).ID == "ListBox3")
            {
                RecordCount = GetData(ListBox4, ListBox3.SelectedValue.Split('E')[ListBox3.SelectedValue.Split('E').Length - 2]);
                ListBox4.Visible = true;
                _CategoryID = ListBox3.SelectedValue;
                CategoryID = "0";
                CategoryID = _CategoryID;
            }
            else if (((ListBox)sender).ID == "ListBox4")
            {
                RecordCount = GetData(ListBox5, ListBox4.SelectedValue.Split('E')[ListBox4.SelectedValue.Split('E').Length - 2]);
                ListBox5.Visible = true;
                _CategoryID = ListBox4.SelectedValue;
                CategoryID = "0";
                CategoryID = _CategoryID;
            }
            else if (((ListBox)sender).ID == "ListBox5")
            {
                RecordCount = GetData(ListBox6, ListBox5.SelectedValue.Split('E')[ListBox5.SelectedValue.Split('E').Length - 2]);
                ListBox6.Visible = true;
                _CategoryID = ListBox5.SelectedValue;
                CategoryID = "0";
                CategoryID = _CategoryID;
            }

            foreach (ListBox item in Panel1.Controls.OfType<ListBox>())
            {
                int loo = Convert.ToInt32(item.ID.Replace("ListBox", ""));
                int current = Convert.ToInt32(((ListBox)sender).ID.Replace("ListBox", ""));

                if (current + 1 < loo)
                {
                    item.Visible = false;
                }

                if (item.Items.Count == 0)
                {
                    item.Visible = false;
                }
            }


            if (RecordCount == 0)
            {
                Button1.Visible = true;
            }
            else
            {
                Button1.Visible = false;
            }
            
        }

       
        private static string _CategoryID = "0";
        public static string CategoryID = "0";
        protected void Button1_Click(object sender, EventArgs e)
        {
              CategoryID = _CategoryID;
        }
    }
}